$(document).ready(function () {
    //打开导航栏

    $('.btn-triger').click(function () {
        $(this).closest('.float-btn-group').toggleClass('open');
    });

    //模型启动 跳转至任务进程页 box需要修改
    $("#submitTask").click(function () {
        console.log(2221112)
        $("#mison").addClass('fade in active');
        $("#home").removeClass('fade in active');
        console.log(2222244545)
        $("#li2").addClass('active');
        $("#li1").removeClass('active');
        $(".addmo1").hide();
        $(".addmo").hide();
    });
    $("#li1").click(function () {
        $("#myTabContent").show();
        console.log(2221112)
        setTimeout(function () {
            $(".addmo1").show();
        }, 200);
        setTimeout(function () {
            $(".addmo").show();
        }, 200);
    });
    $("#li2").click(function () {
        console.log(2221112)
        $(".addmo1").hide();
        $(".addmo").hide();
    });
    //跳出选择文件窗口
    $("#signbtn").click(function () {
        $("#myModal").modal('show');
        console.log(22222)
    });
    //刷新按钮
    $("#rev1").click(function () {
        getParallelModellist();
    });
    //打开并行模型填写参数窗口
    $(".player").click(function () {
        showParallelModel("BKmeans");
    });
    $("#del").click(function () {
        getData();
        console.log(2222)
    });

    $("#sub").click(function () {
        var mi_name = $("#pa_1").val();
        var type_count = $("#pa_2").val();
        var co_num = $("#pa_3").val();
        var ty_min = $("#pa_4").val();
        var ToJson = "'{" + "\"任务名称\":\"" + mi_name + "\",\"聚类类别个数\":\"" + type_count + "\",\"最大迭代次数\":\"" + co_num + "\",\"每个类中的最小个数\":\"" + ty_min + "\"}'";
        console.log(ToJson)
        var su = $('#para_body p').length + $('#para_body input').length;
        console.log(su)
        var jsonArray = newJSONArray();
        var attchJson = newJSONObject();
        for (i = 0; i <= su; i++) {
            attchJson.put("\"" + mi_name + "\"", "0");
        }
        }
    );
    init();
    // var jsonArray = newJSONArray();
    // var attchJson = newJSONObject();
    //
    // attchJson.put("attchId","0");
    //
    // attchJson.put("attchName", "附件0");jsonArray.put(attchJson);jsonArray

});

function init() {
    getParallelModellist();
}

//动态添加的div里填文字

//注册并行模型
function registerParallelModel() {
    // debugger;
    var validateState = true;
    $('#uploadFile').find("#addJar,#addXML").each(function (index, target) {
        if ($(target).val().trim() == "") {
            validateState = false;
        }
    });
    if (!validateState) {
        alert("请完善信息");
        return;
    }
    $("#myModal").modal("hide");
    var form = new FormData();
    form.append("file", $("#addXML")[0].files[0]);
    form.append("file", $("#addJar")[0].files[0]);
    $.ajax({
        url: "/dldsj/parallel/register",
        type: "post",
        data: form,
        dataType: "json",
        processData: false,
        contentType: false,
        success: function (data) {
            debugger;
            if (data.code == "200") {
                alert("并行模型注册成功");
                getParallelModellist();
                return;
            } else {
                alert("并行模型注册失败");
                return;
            }
        },
        error: function (data) {
            $("#myModal").modal("hide");
            alert("并行模型注册失败");
        }
    });
}

//打开并选择XML文件
function selectXML() {
    $("#addXML").trigger("click");
}

//获取并行模型xml文件
function getXMLfile(target) {
    var files = target.files;
    if (files && files.length > 0) {
        xmlFile = files[0]
        $("#xmlInput").val(xmlFile.name);
    } else {
        return;
    }
}

function getJarfile(target) {
    var files = target.files;
    if (files && files.length > 0) {
        jarFile = files[0]
        $("#jarInput").val(jarFile.name);
    } else {
        return;
    }
}

function selectJAR() {
    $("#addJar").trigger("click")

}

//获取单个并行模型参数
function showParallelModel(artifact_id) {
    $.ajax({
        url: "/dldsj/parallel/get/" + artifact_id,
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data.code = "200") {
                data.body.parameters = JSON.parse(data.body.parameters);
                var initdiv = $("#tools_params_modal .modal-content");
                var myTemplate = Handlebars.compile($("#parallel_template").html());
                initdiv.html(myTemplate(data.body));
                $("#tools_params_modal").modal('show');
            } else {
                alert("获取参数失败")
            }
        }
    });
}

function submitClick(artifact_id) {
    closeParallelModel();
    var data = {};
    data.user = "ubt";
    data.customname = $("#input_div_task_name").find("input")[0].value;
    var params = {};
    var i = 1;
    $.each($("#input_div_task_name").find("input"), function (index, item) {
        params[i++] = item.value;
    })
    data.params = params;
    $.ajax({
        url: "/dldsj/parallel/use/" + artifact_id,
        type: "POST",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(data),
        dataType: "json",
        success: function (data) {
            if (data && data.code == "200") {
                var task_result = [];
                task_result.name = data.body;
                addTaskItem(task_result);
                alert("模型提交到后台成功");
            } else {
                alert("模型提交到后台失败")
            }
        }, error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);

        }
    });
}

function closeParallelModel() {
    $("#tools_params_modal").modal("hide");
}

//获取并行模型列表
function getParallelModellist() {
    $.ajax({
        url: "/dldsj/parallel/get",
        type: "GET",
        dataType: "json",
        success: function (data) {
            if (data.code = "200") {
                var body = data.body;
                var initdiv = $("#modelList");
                var myTemplate = Handlebars.compile($("#parallel_lists_template").html());
                initdiv.html(myTemplate(body));
            } else {
                return;
            }
        },
        error: {}
        ,
    });
}